package com.changgou.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.pojo.SkuInfo;
import com.changgou.search.mapper.SkuInfoMapper;
import com.changgou.search.service.EsManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * <dl>
 * <dd>描述: ~节点业务逻辑实现</dd>
 * <dd>创建时间：  11:16 2020/4/5</dd>
 * <dd>创建人： zz</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2020/4/5      guodong       1.0        1.0 Version
 * </pre>
 * </dl>
 */
@Service
public class EsManageServiceImpl implements EsManageService {

    private Logger log = LoggerFactory.getLogger(EsManageServiceImpl.class);
    @Autowired
    private ElasticsearchTemplate template;
    @Autowired
    private SkuFeign skuFeign;
    @Autowired
    private SkuInfoMapper skuInfoMapper;

    /**
     * @Date: 11:25 2020/4/5
     * @Param: []
     * @return: com.changgou.entity.Result
     * @Description: 创建索引库
     **/
    @Override
    public void createIndex() {
        //创建索引库
        template.createIndex(SkuInfo.class);
        //创建类型
        template.putMapping(SkuInfo.class);
    }

    /**
     * @Date: 11:25 2020/4/5
     * @Param: []
     * @return: com.changgou.entity.Result
     * @Description: 删除索引库
     **/
    @Override
    public void delIndex() {
        template.deleteIndex(SkuInfo.class);
    }

    /**
     * @Date: 11:33 2020/4/5
     * @Param: []
     * @return: com.changgou.entity.Result
     * @Description:
     *
     *  需求： 根据spuId导入sku数据
     *
     *  分析：
     *      1.需要changgou_service_goods_api 提供feign接口------>根据spuId导入sku数据
     *
     *
     **/
    @Override
    public void importBySpuId(String spuId) {
        //1.根据spuId查询skuList
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        //2.存入es索引库
        saveSkuListToES(skuList);

    }

    /**
    * @Date: 14:16 2020/4/5
    * @Param: [skuList]
    * @return: void
    * @Description:  保存skuList数据至es索引库
    **/
    private void saveSkuListToES(List<Sku> skuList) {
        if(!CollectionUtils.isEmpty(skuList)){
            log.info("查询所有sku数据大小：{}", skuList.size());
            //1.首先将skuList转为json字符串 ---》再通过parseArray将skuList json字符串转为 SkuInfo list
            //前提要求 ： 属性名称必须一一对应
            List<SkuInfo> skuInfos = JSON.parseArray(JSON.toJSONString(skuList), SkuInfo.class);
            //2.处理specMap特殊属性
            for (SkuInfo skuInfo : skuInfos) {

            /*
                  spec字段  {'颜色': '蓝色','内存'：'4g'}
             */
                Map<String, Object> specMap = JSONObject.parseObject(skuInfo.getSpec(), Map.class);
                skuInfo.setSpecMap(specMap);
            }
            skuInfoMapper.saveAll(skuInfos);
            log.info("所有sku数据导入es索引库完毕");
        }
    }

    /**
     * @Date: 14:14 2020/4/5
     * @Param: []
     * @return: com.changgou.entity.Result
     * @Description:  导入所有sku数据
     **/
    @Override
    public void importAll() {
        //查询所有sku
        List<Sku> skuList = skuFeign.findAll();
        //保存
        saveSkuListToES(skuList);
    }

    /**
     * @Date: 9:19 2020/4/6
     * @Param: [spuId]
     * @return: void
     * @Description: 根据spuId删除sku索引库数据
     **/
    @Override
    public void delBySpuId(String spuId) {
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        if(!CollectionUtils.isEmpty(skuList)){
            for (Sku sku : skuList) {
                String id = sku.getId();
                skuInfoMapper.deleteById(Long.valueOf(id));
            }
        }
    }
}
